﻿using QuMianChaZhi.Contour;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuMianChaZhi.MyContour
{
    public class MyContourImpl
    {

        public static string[] colors = { "006837", "1a9850", "66bd63", "a6d96a", "d9ef8b", "ffffbf", "fee08b", "fdae61", "f46d43", "d73027", "a50026" };

        public static Color HexToColor(string hex)
        {
            byte br = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            byte bg = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
            byte bb = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
            //byte cc = byte.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
            int r = br;
            int g = bg;
            int b = bb;
            //int a = cc / 255;
            return Color.FromArgb(r, g, b);
        }

        public List<Color> RainbowColors { get; private set; }

        List<double> SameHeightArray = new List<double>();

        List<List<double>> InputDatas = new List<List<double>>();

        double BandWidth = 0;


        //double[,] InputData;
        public MyContourImpl()
        {
            //InputData = rawdata;

            RainbowColors = new List<Color>();
            foreach(var cl in colors)
            {
                RainbowColors.Add(HexToColor(cl));
            }
        }


        public ReadInTemp ParseSurfaceRawData(double[,] rawdata, int width, int height)
        {
            double[][] rets = new double[height][];


            double max = double.MinValue;
            double min = double.MaxValue;

            Dictionary<string, int> elementCount = new Dictionary<string, int>();

            SortedDictionary<int, int> ValueCount = new SortedDictionary<int, int>();

            double[] arrayRawDatas = new double[width * height];
            int arrayIndex = 0;

            int xIndex = 0;
            for (int row = 0; row < height; row++)
            {


                rets[xIndex] = new double[width];

                InputDatas.Add(new List<double>());

                for (int yIndex = 0; yIndex < width; yIndex++)
                {
                    double zValueIn = rawdata[row, yIndex];


                    InputDatas[xIndex].Add(zValueIn);

                    if (zValueIn > max)
                    {
                        max = zValueIn;
                    }

                    if (zValueIn < min)
                    {
                        min = zValueIn;
                    }
                }

                xIndex++;

            }

            BandWidth = (max - min) / 10.0;

            SameHeightArray.Add(min);
            for (int index = 1; index <= 8; index ++)
            {
                double value = min + index * BandWidth;
                SameHeightArray.Add(value);
            }
            SameHeightArray.Add(max);


            ReadInTemp retTemp = new ReadInTemp();
            retTemp.RawData = rets;
            retTemp.RawDataArray = arrayRawDatas;
            retTemp.Max = max;
            retTemp.Min = min;
        

            retTemp.W = width;
            retTemp.H = height;

            retTemp.ValueCount = ValueCount;

            return retTemp;
        }


        public List<List<List<List<double>>>> ISOBandAreas { get; private set; }

        public void IsoBand()
        {
            int extentWidth = 240;
            int extentHeight = 240;

            int extentYmin = 0;
            int extentXmin = 0;

            int dataWidthHeight = 80;

            int squareWidth = extentWidth / (dataWidthHeight - 1);
            int squareHeight = extentHeight / (dataWidthHeight - 1);

            ISOBandAreas = new List<List<List<List<double>>>>();

            for (int kIndex = 0; kIndex < SameHeightArray.Count - 1; kIndex++)
            {
                MarchingSquares MS = new MarchingSquares();

                var rets = MS.IsoBand(InputDatas, SameHeightArray[kIndex], BandWidth);

                ISOBandAreas.Add(rets);

                //3 值话。
                //int[,] stateMatrix = new int[80,80];

                


            }
        }

    }
}
